﻿Imports ajWebClient = ajayumi.Net.VBasic.WebClient
Imports ajayumi.develop.win32.WebResourcesCatcher.Service
Imports System.Net

Public Class ResourceDownloader
    Inherits WebClient


    Private m_WebResInfo As WebResourceInfo
    Public ReadOnly Property WebResInfo() As WebResourceInfo
        Get
            Return m_WebResInfo
        End Get
    End Property


    Public Sub New(webResInfo As WebResourceInfo)
        MyBase.New()

        Me.m_WebResInfo = webResInfo

    End Sub

    Public Sub [Start]()
        Me.DownloadFileAsync(New Uri(WebResInfo.Url), WebResInfo.SavePath)
        Me.m_WebResInfo.ResStatus = ResourceStatus.Downloading
        Me.m_WebResInfo.Information = String.Empty
    End Sub

    Protected Overrides Sub OnDownloadProgressChanged(e As System.Net.DownloadProgressChangedEventArgs)
        MyBase.OnDownloadProgressChanged(e)
        Me.m_WebResInfo.ProgressPercentage = e.ProgressPercentage
    End Sub

    Protected Overrides Sub OnDownloadFileCompleted(e As System.ComponentModel.AsyncCompletedEventArgs)
        MyBase.OnDownloadFileCompleted(e)
        If e.Error Is Nothing Then
            Me.m_WebResInfo.ResStatus = ResourceStatus.Downloaded
            Me.m_WebResInfo.Information = String.Empty
            Me.AfterDownloadCompleted()
        Else
            Me.m_WebResInfo.ResStatus = ResourceStatus.Error
            Me.m_WebResInfo.Information = e.Error.Message
        End If
    End Sub

    Protected Overridable Sub AfterDownloadCompleted()

    End Sub

End Class
